package cn.fz.simple;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 拉模式-指定获取一个queue消息
 */
public class LitePullConsumerAssign {
    public static void main(String[] args) throws MQClientException {
        DefaultLitePullConsumer consumer = new DefaultLitePullConsumer("LitePullConsumer");
        consumer.setNamesrvAddr("localhost:9876");
        consumer.start();
        Collection<MessageQueue> messageQueues = consumer.fetchMessageQueues("Simple");
        ArrayList<MessageQueue> messageQueues1 = new ArrayList<>(messageQueues);
        consumer.assign(messageQueues1);
        consumer.seek(messageQueues1.get(0),10);
        while (true){
            List<MessageExt> messageExtList = consumer.poll();
            System.out.println("消息拉取成功");
            messageExtList.forEach(n-> System.out.println("消息消费成功 = " + n));
        }
    }
}
